package demo1;


import java.util.List;

public class MySingleList {
    static class ListNode {
        public int val;//节点的值域
        public ListNode next;//下一个节点的地址

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void creatList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(11);
        ListNode node3 = new ListNode(34);

        node1.next = node2;
        node2.next = node3;

        this.head = node1;//头节点是指向node1的
    }

    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找是否包含关键字key是否在单链表中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int date) {
        ListNode node = new ListNode(date);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = head;
        //链表中只有头节点，则直接让head指向node,添加完毕
        if (cur == null) {
            head = node;
            return;
        }
        //链表中不止有头结点，则找到尾节点，添加节点node
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    //插入
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("插入位置不合法");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = findIndexSubOne(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    private ListNode findIndexSubOne(int index) {
        ListNode cur = head;
        while (index - 1 != 0) {//特别聪明的循环条件  2位置插入，cur移动2次 0 1
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //删除 值为key 的第一个节点
    public void remove(int key) {
        if (head == null) {
            return;
        }
        //单独删除头结点
        if (head.val == key) {
            head = head.next;
            return;
        }

        ListNode cur = searchPre(key);
        if (cur == null) {
            System.out.println("没有你要删除的数字");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    //找到当前key的前驱
    private ListNode searchPre(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除 所有值为key 的节点
    public void removeAll(int key) {
        if (head == null) {
            return;
        }
        ListNode cur = head.next;
        ListNode pre = head;
        while (cur != null) {
            if (cur.val == key) {
                pre.next = cur.next;
                cur = cur.next;
            } else {
                pre = cur;
                cur = cur.next;
            }
        }
        if (head.val == key) {
            head = head.next;
        }
    }

    public void clear() {
        this.head = null;
    }


    //反转
    public ListNode reverseList() {
        if (head == null || head.next == null) {
            return head;
        }

        //cur从第二个节点开始
        ListNode cur = head.next;
        //先把头结点next置空
        head.next = null;

        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return cur;
    }

    //从指定位置 开始打印 newhead
    public void display(ListNode newhead) {
        ListNode cur = newhead;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    /* 给定一个带有头结点 head 的非空单链表，返回链表的中间结点。如果有两个中间结点，则返回第二个中间结点。*/
    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    /*输入一个链表，输出该链表中倒数第k个结点*/
    public ListNode FindKthToTail(int k) {
        if (k < 0 || head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < k - 1; i++) {
            fast = fast.next;
            if (fast == null) {
                return null;
            }
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }


    /*将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的*/
    public ListNode partition(ListNode pHead, int x) {
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        ListNode cur = pHead;
        while (cur != null) {//没有遍历完整个列表
            if (cur.val < x) {
                //第一次插入
                if (bs == null) {
                    bs = be = cur;
                } else {
                    //尾插法：保证原来顺序不变
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        //第一个段没有数据
        if (bs == null) {
            return as;
        }
        be.next = as;
        //防止 最大的数据 不是最后一个
        if (as != null) {
            ae.next = null;
        }
        return bs;
    }

    /*链表的回文结构*/
    public boolean chkPalindrome(ListNode A) {
        //1、找中间位置
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        //2、开始翻转
        ListNode cur=slow.next;
        while(cur!=null){
            ListNode curNext=cur.next;//记录一下下一个节点
            cur.next=slow;
            slow=cur;
            cur=curNext;
        }
        //3、此时翻转完成，开始判断是否回文
        while(head!=slow){
            if(head.val!=slow.val){
                return false;
            }
            if(head.next==slow){
                return true;
            }
            head=head.next;
            slow=slow.next;
        }
        return true;
    }

    //相交链表
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //1、分别求2个链表的长度
        int lenA=0;
        int lenB=0;

        ListNode pL=headA;//假设pL所指向的链表是最长的链表
        ListNode pS=headB;//假设pS所指向的链表是最短的链表
        while(pL!=null){
            lenA++;
            pL=pL.next;
        }
        while(pS!=null){
            lenB++;
            pS=pS.next;
        }

        //让pL和pL重新指向头结点，否则两个都为null
        pL=headA;
        pS=headB;

        //2、求长度差值
        int len=lenA-lenB;

        //3、修正指向和len的差值
        if(len<0){
            pL=headB;
            pS=headA;
            len=lenB-lenA;
        }
        //此时我们能保证pL一定指向最长的链表，pS一定指向最短的链表 len一定是一个正数

        //4、让最长的链表先走差值步
        while(len!=0){
            pL=pL.next;
            len--;
        }

        //5、相遇点
        while(pL!=pS){
            pL=pL.next;
            pS=pS.next;
        }
        return pL;
    }

//    给定一个链表，判断链表中是否有环
    public boolean hasCycle(ListNode head) {
        if(head==null) return false;
        ListNode fast=head;
        ListNode slow=head;

        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
                return true;
            }
        }
        return false;
    }

    //创建一个有环链表
    public void createLoop(){
        ListNode cur=head;
        while(cur.next!=null){
            cur=cur.next;
        }
        //cur.next=null;
        cur.next=head.next;
    }

   /* 给定一个链表，返回链表开始入环的第一个节点。 如果链表无环，则返回NULL*/
    /*1、设起始点到入口点的距离为X
      2、环的长度为 C
      3、假设最快时间相遇
      4、相遇点 到 入口点 的距离为Y
      fast的路程 = X+C+(C-Y)
      slow的路程 = X+(C-Y)
      又因为 fast的速度 是 slow 速度的两倍
      得 X==Y

      X=(N-1)C+Y
     */
   public ListNode detectCycle(ListNode head) {
       if(head==null) return null;
       ListNode fast=head;
       ListNode slow=head;

       while(fast!=null&&fast.next!=null){
           fast=fast.next.next;
           slow=slow.next;
           if(fast==slow){
               break;
           }
       }
       if(fast==null||fast.next==null){
           return null;
       }
       fast=head;
       while(fast!=slow){
           fast=fast.next;
           slow=slow.next;
       }
       return fast;
   }

   //删除重复节点，返回头结点
   public ListNode deleteDuplication(ListNode pHead){
       ListNode cur=pHead;
       ListNode newHead=new ListNode(-1);
       ListNode tmpHead=newHead;
       //遍历链表得每个节点
       while(cur!=null){
           if(cur.next!=null&&cur.val==cur.next.val){
               while(cur.next!=null&&cur.val==cur.next.val){
                   cur=cur.next;
               }
               cur=cur.next;
           }else{
                tmpHead.next=cur;
                tmpHead=tmpHead.next;
                cur=cur.next;
           }
       }
       tmpHead.next=null;//否则若新链表最后一个节点不是原链表中最后一个节点，则会把原链表中后面节点连上去
       return newHead.next;
   }
}























